	clear all
	set more off
	local directory ""

	**********************************************************************************************

	global utility "CES" // "logutility" // "leontieff" // "linear"		
	global rho 3
	
  	/***************************************/
	/***************************************/
	
	global NSimul 1000
	
	global Nparts 20
	global h = 1/$Nparts	
	if "$utility" == "linear" {
		global Nparts = $Nparts + 1
	}
	global N = $Nparts + 1
	global Nm1 = $N - 1
	global Nm2 = $N - 2
	
  	/***************************************/
	/***************************************/
	
	use prim_key budget_order highDMA avoidance endowment price_tails outside_option post_heads max_heads max_tails post_tails treatment_arm_4 using "`directory'/main_data" if treatment_arm_4 == 1, clear

	preserve

  	/***************************************/
	/*  		Estimating Lambda		   */ 
	/***************************************/
	
		/***************************************/
		/*   			scalers  			   */
		/***************************************/
		
		bysort price_tails: egen median = median(endowment)
		gen lnphi = ln(endowment/median)
		drop median

		bysort price_tails: egen median = median(outside_option)
		gen lntheta = ln(outside_option/median)
		drop median
		gen x = lnphi - lntheta
		
		/***************************************/
		/*   		 Estimation			       */
		/***************************************/
		
		gen low  = (1 - highDMA) * x   
		gen high = highDMA 		 * x  
			
		gen opt_in = 1 - avoidance
		egen group = group(price_tails highDMA) 

		clogit opt_in low high, group(group)
		gen baseline = 1/_b[low]
		gen skill   = (1/_b[high]) - (1/_b[low])
		
		summ baseline
		global lambda_low = r(mean)
		summ skill
		global lambda_high = r(mean) + $lambda_low	
		
	restore
	
		/***************************************/
		/*  		Storing Lambda		 	   */ 
		/***************************************/
		
		gen lambda = .
			replace lambda = $lambda_low	if highDMA == 0
			replace lambda = $lambda_high	if highDMA == 1
	
	
	/************************************************************************/
	/*  					PARTITIONING ACTION SPACE						*/ 
	/* 		Action Space Inside Portfolio Defined in Terms of 				*/
	/*					Payoff if heads/ Max Payoff if heads    			*/ 
	/************************************************************************/
	
 	/************************************************************************/
	/* 						Identifying Action Taken	    				*/ 
	/************************************************************************/
		
	cap drop Y
	gen Y = post_heads/max_heads
		
	*******************************
	*** Discretizing Action Space *
	*******************************
	
	forvalues i = 1/$Nm1 {
		gen Chosen`i' = 0		
	}	
		
	if "$utility" == "linear" {
		
		replace Chosen1  = 1			if Y <= $h/2 
		forvalues i = 2/$Nm2 {
			local j = $h/2 + ($h * (`i' - 2))
			local k = `j' + $h
			replace Chosen`i' = 1	if Y > `j' & Y <= `k'
		}
		replace Chosen$Nm1 = 1		if Y > 1 - $h/2 & Y < .
		
		clonevar Chosen$N = avoidance
		
	}
	
	else {
		
		replace Chosen1  = 1			if Y <= $h 
		forvalues i = 2/$Nm2 {
			local j = $h * (`i' - 1)
			local k = `j' + $h
			replace Chosen`i' = 1	if Y > `j' & Y <= `k'
		}
		replace Chosen$Nm1 = 1		if Y > 1 - $h & Y < .
		
		clonevar Chosen$N = avoidance
		
	}
	
	/****************************************************/
	/*  Calculating Payoffs Associated with Each Action */ 
	/****************************************************/
		
	forvalues i = 1/$N {
		gen heads`i' = .
	}	
	forvalues i = 1/$N {
		gen tails`i' = .
	}			
	
	if "$utility" == "linear" {
		
		replace heads1 = 0
		replace tails1 = max_tails
		forvalues i = 2/$Nm2 {
			local j = $h * (`i' - 1)
			replace heads`i' = `j' 		 * max_heads // payoff if heads
			replace tails`i' = (1 - `j') * max_tails // payoff if tails 
		}
		replace heads$Nm1 = max_heads
		replace tails$Nm1 = 0		

	}
	
	else {
		
		forvalues i = 1/$Nm1 {
			local j = $h * (`i' - 1/2)
			replace heads`i' = `j' 		 * max_heads // payoff if heads
			replace tails`i' = (1 - `j') * max_tails // payoff if tails 
		}
		
	}
		
	replace heads$N = outside_option
	replace tails$N = outside_option		
			
		/********************************************/
		/* Calculating Utility of Different Actions */ 
		/********************************************/
			
		if "$utility" == "linear" {
			
			forvalues i = 1/$N {
					
				gen v`i' = (heads`i' + tails`i')/2
					
			}
					
		}

		if "$utility" == "leontieff" {
			
			forvalues i = 1/$N {
					
				egen v`i' = rowmin(heads`i' tails`i')
					
			}
					
		}
			
		if "$utility" == "CES" {
				
			forvalues i = 1/$N {
				
				gen v`i' = (.5 * (heads`i'^(1-$rho)) + .5 * (tails`i'^(1-$rho))) ^ (1/(1-$rho))
					
			}
				
		}

		if "$utility" == "logutility" {
				
			forvalues i = 1/$N {
				
				gen v`i' = (ln(heads`i') + ln(tails`i'))/2	
					
			}
				
		}
		
		/****************************************/
		/* Calculating Utility of Chosen Action */ 
		/****************************************/
	
		gen utility_chosen = .
		
		forvalues i = 1/$N {
					
			replace utility_chosen = v`i'	if Chosen`i' == 1
					
		}
		
		/***************************************/
		/* 	 Calculating CEQ of Chosen Action  */ 
		/***************************************/
	
		gen CEQ_chosen = .
		
			if "$utility" == "linear" | "$utility" == "leontieff" | "$utility" == "CES" {
			
				replace CEQ_chosen = utility_chosen
					
			}

			if "$utility" == "logutility" {
			
				replace CEQ_chosen = exp(utility_chosen)
					
			}
			
		/**************************************************************/
		/* Identifying Optimal Action Inside the Investment Portfolio */ 
		/**************************************************************/

		if "$utility" == "leontieff" {	
		
			gen optimalY = max_tails/(max_heads + max_tails)
			
		}	
		
		if "$utility" == "logutility" {	
		
			gen optimalY = .5
			
		}	
		
		if "$utility" == "CES" {	
		
			gen optimalY = 1/( 1 + (max_heads/max_tails)^(1-1/$rho))
			
		}	
		
		gen optimal = .
			
		if "$utility" == "linear" {
					
				replace optimal = $Nm1  if max_heads >= max_tails    
				replace optimal = 1 	if max_heads <  max_tails    
					
		}
			
		else {
			
			replace optimal  = 1			if optimalY <= $h 
			forvalues i = 2/$Nm2 {
				local j = $h * (`i' - 1)
				local k = `j' + $h
				replace optimal = `i'	if optimalY > `j' 		& optimalY <= `k'
			}
			replace optimal = $Nm1		if optimalY > 1 - $h 	& optimalY < .
			
			drop optimalY
			
		}	
		
		/*************************************************************************/
		/* Calculating Utility of Optimal Action Inside the Investment Portfolio */ 
		/*************************************************************************/
	
		gen utility_optimal = .
		
		forvalues i = 1/$Nm1 {
			replace utility_optimal = v`i'	if optimal == `i'
		}
		
		/************************************************************************************/
		/* Does the Outside Option Dominate Optimal Action Inside the Investment Portfolio? */ 
		/************************************************************************************/
	
		replace optimal = $N				if v$N > utility_optimal
		replace utility_optimal = v$N		if v$N > utility_optimal
			
		/***************************************/
		/* 	 Calculating CEQ of Optimal Action */ 
		/***************************************/
	
		gen CEQ_optimal = .
		
			if "$utility" == "linear" | "$utility" == "leontieff" | "$utility" == "CES" {
			
				replace CEQ_optimal = utility_optimal
					
			}

			if "$utility" == "logutility" {
			
				replace CEQ_optimal = exp(utility_optimal)
					
			}
			
		/***************************************/
		/* 	 Calculating Money on the Table    */ 
		/***************************************/
	
		gen actual_MOTT = CEQ_optimal - CEQ_chosen
	
		mean actual_MOTT if highDMA == 0
		global obs_low    = _b[actual_MOTT]
	
		mean actual_MOTT if highDMA == 1
		global obs_high =_b[actual_MOTT] - $obs_low
		
		/*****************************************************************************/
		/* Calculate Probabilities of Different Actions as Predicted by the RI Model */ 
		/*****************************************************************************/
	
		*** Log Transformation		
		forvalues i = 1/$N {
			gen lnv`i' = ln(v`i')
		}
			
		*** Summing over exp{lnv_j/lambda} for all actions j inside the investment portfolio
		gen kappa = 0
		forvalues j = 1/$N {
			replace kappa = kappa + exp(lnv`j'/lambda)
		}	
			
		forvalues i = 1/$N {
			gen Pmodel`i' = exp(lnv`i'/lambda) / kappa
		}	
	
		/*****************************************************************************/
		/*      Simulating Actions by Drawing using Model Action Probabilities       */ 
		/*****************************************************************************/
			
		keep Pmodel* highDMA CEQ_optimal v*
	
		gen a = -Pmodel$N
		gen b = 1-Pmodel$N
		gen lb1 = 0
		gen ub1 = Pmodel1
		forvalues i = 2/$Nm1 {
			local j = `i' - 1
			gen lb`i' = ub`j'
			gen ub`i' = lb`i' + Pmodel`i'
		}	
		gen lb$N = -Pmodel$N
		gen ub$N = 0
				
		
		drop Pmodel*
		
		compress				
		
		expand $NSimul
		
		set seed 21562534
		
		gen double u = (b-a)*runiform() + a
		
		forvalues i = 1/$N {
			gen Simul`i' = 0
		}
		
		forvalues i = 1/$N {
			replace Simul`i' = 1	if u > lb`i' & u < ub`i'
		}
		
		/*******************************************/
		/* Calculating Utility of Simulated Action */ 
		/*******************************************/
	
		gen utility_simul = .
		
		forvalues i = 1/$N {
					
			replace utility_simul = v`i'	if Simul`i' == 1
					
		}
		
		/***************************************/
		/* Calculating CEQ of Simulated Action */ 
		/***************************************/
	
		gen CEQ_simul = .
		
			if "$utility" == "linear" | "$utility" == "leontieff" | "$utility" == "CES" {
			
				replace CEQ_simul = utility_simul
					
			}

			if "$utility" == "logutility" {
			
				replace CEQ_simul = exp(utility_simul)
					
			}
		
		/***************************************/
		/* 	 Simulating Money on the Table     */ 
		/***************************************/
	
		gen simul_MOTT = CEQ_optimal - CEQ_simul
		
		dis $obs_low
		dis $obs_high	
		
		reg simul_MOTT highDMA
